package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.service.DishService;
import com.itheima.service.SetmaelDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/10
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmaelDishService setmaelDishService;

    @Transactional
    public void saveWithDish(SetmealDto setmeal) {
        //先将菜品信息保存到菜品表
        this.save(setmeal);
        //获取所有的口味信息
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //循环将每个口味对应的菜品id赋上值
       setmealDishes.stream().map((item) -> {
                    item.setSetmealId(setmeal.getId());
                    return item;
                }
        ).collect(Collectors.toList());
        //将口味保存到口味表
        setmaelDishService.saveBatch(setmealDishes);
    }
}
